<?php

use yii\helpers\Url;
use yii\helpers\Html;
use website\helpers\Render;
use common\models\Employment;

$this->addCrumbs('雇佣管理');
$this->title = $employment ? '修改雇佣信息' : '雇佣设计师';
?>
<style>
    #designer,
    #flyer-create {padding:20px;background-color:#eee;}
    #designer {line-height:30px;}
    #designer img {float:left;width:120px;height:120px;margin-right:20px;}
    #order-title {height:30px;line-height:30px;margin-bottom:30px;font-size:18px;}
</style>

<div class="contenter">
    <?= $this->render('/user/navigator', ['active' => 'employment']) ?>
    
    <div id="user-contenter">
        <div class="box-shadow" id="sub-contenter">
            <div class="data-title"><span class="h3"><?= $this->title ?></span></div>
            <div class="data-content flyer-form">
                <div id="designer">
                    <img src="<?= Render::upload($designer->photo) ?>">
                    <p>
                        <span class="fs-24px mr-20px"><?= $designer->nickname ?></span>
                        <span class="mr-20px">综合评价：<span class="star cl-red"><?= Render::star($designer->star) ?></span></span>
                        <span class="mr-20px">薪资：<?= Render::amount($designer->salary) ?>元/月</span>
                    </p>
                    <p>擅长类目：<span class="categories"><?= $designer->categoriesString ?></span></p>
                    <p>擅长风格：<span class="styles"><?= $designer->stylesString ?></span></p>
                    <p>擅长软件：<?= $designer->skills ?></p>
                </div>
                <form class="mt-20px" id="flyer-create" onsubmit="return false;">
                    <div id="order-title"><i class="icon-th-list cl-red"></i> 填写订单信息</div>
                    <div class="form-item checker">
                        <div class="input-title">雇佣类型：</div>
                        <div class="input-inline gender clear">
                            <input name="type" value="1" type="radio" flyer="radio" title="在线定制">
                            <input name="type" value="2" type="radio" flyer="radio" title="在线全职" checked>
                        </div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">工作内容：</div>
                        <div class="input-block"><textarea class="flyer-textarea" name="describe"><?= Render::value($employment, 'describe') ?></textarea></div>
                    </div>
                    <div class="form-item">
                        <div class="item-inline">
                            <div class="input-title">联系方式：</div>
                            <div class="input-inline gender clear">
                                <input name="link-type" value="1" type="radio" flyer="radio" title="注册手机" data-mobile="<?= Yii::$app->user->mobile ?>" checked>
                                <input name="link-type" value="2" type="radio" flyer="radio" title="其他" data-mobile="<?= Render::value($employment, 'contact_mobile') ?>">
                            </div>
                        </div>
                        <div class="item-inline checker">
                            <div class="input-inline"><input class="flyer-input" name="contact_mobile" type="text" value="<?= $employment ? Render::value($employment, 'contact_mobile') : Yii::$app->user->mobile ?>"></div>
                            <div class="input-inline input-mid">设计师将通过该号码联系您</div>
                        </div>
                    </div>
                    <div class="form-item">
                        <div class="item-inline checker">
                            <div class="input-title">入职时间：</div>
                            <div class="input-inline"><input class="flyer-input flyer-date" name="begin" type="text" value="<?= $employment ? date('Y-m-d', strtotime($employment->begin)) : date('Y-m-d', strtotime('+1 days')) ?>"></div>
                        </div>
                        <div class="item-inline checker">
                            <div class="input-title">薪资：</div>
                            <div class="input-inline w-120px">
                                <input class="flyer-input w-80px pr-10px mr-10px tr" name="hires" type="text" value="<?= Render::value($employment, 'hires') ?>" style="display:inline-block;">
                                <span>元</span>
                            </div>
                        </div>
                        <div class="item-inline checker">
                            <div class="input-title">期限：</div>
                            <div class="input-inline w-100px">
                                <input class="flyer-input w-60px pr-10px mr-10px tr" name="period" type="text" value="<?= Render::value($employment, 'period', 3) ?>" style="display:inline-block;">
                                <span id="period-unit">月</span>
                            </div>
                        </div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">薪酬总计：</div>
                        <div class="input-inline input-mid cl-red fs-16px" id="total-amount">0.00 元</div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title"></div>
                        <div class="input-inline"><button class="flyer-button normal border-round" id="save-button" type="submit">保 存</button></div>
                    </div>
                    <textarea id="flyer-create-json" data-form="#flyer-create" style="display:none;"><?= Employment::checker() ?></textarea>
                    <input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
                    <input type="hidden" name="designer_id" value="<?= $designer->id ?>">
                </form>
            </div>
        </div>
    </div>
</div>
<div id="search" style="display:none;">
    <?= Render::select('style', \common\models\Design::$styleSelector, null) ?>
    <select id="category"></select>
</div>

<script src="<?= Render::static('art-template/template.js') ?>"></script>
<script src="<?= Render::static('flyer/flyer.class.js') ?>"></script>
<script src="<?= Render::static('flyer/checker.class.js') ?>"></script>
<script src="<?= Render::static('flyer/tabler.class.js') ?>"></script>
<script src="<?= Render::static('flyer/tableHandler.class.js') ?>"></script>
<script src="<?= Render::static('flyer/categorier.class.js') ?>"></script>
<script src="<?= Render::static('system/category-design.data.js') ?>"></script>
<script>
    var checkerClass = new checker();
    var categorierClass = new categorier();
    $(document).ready(function() {
        categorierClass.init({ fix: false, dom: '#category', data: DesignCategories, relate: DesignCategoriesRelation, start: '<?= Yii::$app->params['designCategories'] ?>' });
        // 类型选择
        $('input[name=type][value=<?= Render::value($employment, 'type') ?>]').click();
        // 表单美化
        (new flyer).init({ form: '#flyer-create' });
        // 表单数据验证
        checkerClass.init({ ruleDom: '#flyer-create-json' });
        // 雇佣类型单位
        $('input[name=type]').bind('change', function() {
            if( ! $(this).is(':checked')) {
                return true;
            }
            if($(this).val() == '<?= Employment::TypeCustomized ?>') {
                $('#period-unit').html('天');
            }
            else {
                $('#period-unit').html('月');
            }
        });
        $('input[name=link-type]').bind('change', function() {
            if( ! $(this).is(':checked')) {
                return true;
            }
            $('input[name=contact-mobile]').val($(this).data('mobile'));
        });
        // 薪酬总计统计
        $('input[name=period], input[name=type], input[name=hires]').bind('change', function() {
            var period = $('input[name=period]').val();
            var type = $('input[name=type]:checked').val();
            var hires = $('input[name=hires]').val();
            var amount = '0.00';
            if(type == '<?= Employment::TypeCustomized ?>') {
                amount = hires;
            }
            else {
                if(period && hires) {
                    amount = hires * period;
                }
            }
            $('#total-amount').html(fmoney(amount, 2) + ' 元');
        });

        // 保存按钮
        tableHandler.requestSingle({
            button: '#save-button', isConfirm: false, isShadow: false, isAlert: false,
            url: "<?= Url::to('@web/employer/employment?id='.Render::value($employment, 'id')) ?>",
            beforeRequest: function(param) {
                if( ! checkerClass.validate()) {
                    layer.msg('请先填写完整招聘信息');
                    return false;
                }
                param.data = $('#flyer-create').serialize();
                $('#save-button').attr('disabled', true);
            },
            requestFail: function(param) {
                $('#save-button').removeAttr('disabled');
            },
            requestSuccess: function (param) {
                $('#save-button').removeAttr('disabled');
                layer.alert(param.response.message, { icon: 6 }, function() {
                    window.location.href = "<?= Url::to('@web/employer/employment-list') ?>";
                });
            }
        });
        // 名称显示
        tableHandler.renderCategory({ category: $('#designer .styles'), select: '#search select[name=style]', color: false, splite: '，', default: '--' });
        tableHandler.renderCategory({ category: $('#designer .categories'), color: false, splite: '，', default: '--', functionName: function(id) {
            return categorierClass.renderTitles(id);
        }});
    });
</script>